from django.http import HttpResponse, JsonResponse, FileResponse, HttpResponseNotFound
from thirdparty.juhe_data import juhe_weather
import json


def weather(request):
    if request.method == "GET":
        city = request.GET.get("city", "上海")
        weather_data = juhe_weather(city=city)
        if weather_data:
            return JsonResponse(data=weather_data, status=200, safe=False)
        else:
            return HttpResponseNotFound(content="city not found")
    elif request.method == "POST":
        ret_response_data_list = []
        received_body = json.loads(request.body)
        cities = received_body.get("cities")
        for city in cities:
            result = juhe_weather(city)
            ret_response_data_list.append(result)
        return JsonResponse(data=ret_response_data_list, safe=False, status=200)
